package server;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 *功能描述:把请求信息封装成request对象（根据inputStream输入流封装）
 * @author zj
 * @date 2021/7/15
 */
public class Request {
    //url 路径 /index.htm /find/xxx
    private String url;

    //请求类型 get请求 post请求
    private String method;

    private InputStream inputStream;

    private String host;  // 例如 localhost

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }


    public Request(){

    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        //获取输入流
        //获取输入信息长度
        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }
        byte[] messageArr = new byte[count];
        inputStream.read(messageArr);
        String s = new String(messageArr);
        //请求头信息
//        Host: 192.168.1.7:8080
//        Connection: keep-alive
//        Cache-Control: max-age=0
//        Upgrade-Insecure-Requests: 1
//        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
//        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
//        Accept-Encoding: gzip, deflate
//        Accept-Language: zh-CN,zh;q=0.9
        //获取请求头第一行信息
        String s1 = s.split("\\n")[0];
        String[] s2 = s1.split(" ");

        String s3 = s.split("\\n")[1];
        this.host = s3.split(" ")[1].split(":")[0];
        this.method = s2[0];
        this.url = s2[1];
        System.out.println("=============》》url： " + url);
        System.out.println("=============》》method： " + method);
    }
}
